package com.ruoyi.syf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.ConvertField;
import com.ruoyi.common.annotation.ConvertFieldItem;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.syf.common.constant.HolidayType;
import com.ruoyi.syf.domain.HolidayInfo;
import com.ruoyi.syf.domain.vo.HolidayInfoVo;
import com.ruoyi.syf.mapper.HolidayInfoMapper;
import com.ruoyi.syf.service.HolidayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class HolidayInfoServiceImpl extends ServiceImpl<HolidayInfoMapper, HolidayInfo>  implements HolidayInfoService {

    @Autowired
    private HolidayInfoMapper holidayInfoMapper;

    @ConvertField({
            @ConvertFieldItem(fieldName = "type", assistFieldName = "typeStr",
                    constantValue = {"0:工作日", "1:周末", "2:节日", "3:调休"}),
    })
    @Override
    public List<HolidayInfoVo> selectHolidayInfoList(HolidayInfoVo vo) {
        //处理日期
        Integer year = vo.getYear();
        Integer month = vo.getMonth();
        GregorianCalendar calendar = new GregorianCalendar();
        // 重置时间为当月月初
        calendar.set(GregorianCalendar.YEAR, year);
        calendar.set(GregorianCalendar.MONTH, month - 1);
        calendar.set(GregorianCalendar.DAY_OF_MONTH, 1);
        // 获取当月月初的week 需要注意的是  1代表着周末
        int week = calendar.get(GregorianCalendar.DAY_OF_WEEK);
        week = week == 1 ? 7 : week - 1;
        // 本月日历展示的起始日期 例: 2023-08-27
        calendar.add(GregorianCalendar.DAY_OF_YEAR, -week);
        vo.setStartDate(DateUtils.parseDate(DateUtils.dateTime(calendar.getTime())));
        // 本月日历展示的结束日期 例: 2023-10-07
        calendar.add(GregorianCalendar.DAY_OF_YEAR, 41);
        vo.setEndDate(DateUtils.parseDate(DateUtils.dateTime(calendar.getTime())));
        List<HolidayInfoVo> list = holidayInfoMapper.selectHolidayInfoList(vo);
        list.forEach(item -> {
            item.setIsGray(item.getMonth().equals(month) ? 0 : 1);
            if (vo.getType() == null){
                item.setIsShow(true);
            }else {
                item.setIsShow(item.getType().equals(vo.getType()));
            }
        });
        return list;
    }

    @Override
    public boolean week(Long id) {
        HolidayInfo info = getById(id);
        info.setType(HolidayType.WEEK.getCode());
        return updateById(info);
    }

    @Override
    public boolean holiday(Long id) {
        HolidayInfo info = getById(id);
        info.setType(HolidayType.HOLIDAY.getCode());
        return updateById(info);
    }

    @Override
    public boolean adjust(Long id) {
        HolidayInfo info = getById(id);
        info.setType(HolidayType.ADJUST.getCode());
        return updateById(info);
    }

    @Override
    public boolean ifHoliday(Date date) {
        int type = holidayInfoMapper.getTypeByCurdate(date);
        return type == HolidayType.HOLIDAY.getCode() || type == HolidayType.WEEKEND.getCode();
    }

    @Override
    public boolean ifWeek(Date date) {
        int type = holidayInfoMapper.getTypeByCurdate(date);
        return type == HolidayType.WEEK.getCode() || type == HolidayType.ADJUST.getCode();
    }

}
